﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExtensionControls;

namespace ExtensionControls
{
    public partial class ExtensionGrid<T,C> :DataGridView where T : IViewModel<C>
    {
        private BaseViewModelCollection<T, C> _Collection;

        public BaseViewModelCollection<T, C> Collection
        {
            set
            {
                this._Collection = value;
            }
            get
            {
                return this._Collection;
            }
        }

        public ExtensionGrid()
        {
            InitializeComponent();
            this._Collection = null;
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        public void loadData()
        {
            List<ExtensionGridColumnHeader> columnHeaders = this._Collection.GetColumnsType();
            foreach (ExtensionGridColumnHeader header in columnHeaders)
            {
                switch (header.ColumnType)
                {
                    case EControl.TEXT:
                    {
                        DataGridViewTextBoxColumn addingColumn = new DataGridViewTextBoxColumn();
                        addingColumn.HeaderText = header.ColumnLabel;
                        this.Columns.Add(addingColumn);
                        break;
                    }
                    case EControl.OBJECT:
                    {
                        DataGridViewColumn addingColumn = new DataGridViewTextBoxColumn();
                        addingColumn.Visible = false;
                        addingColumn.HeaderText = header.ColumnLabel;
                        this.Columns.Add(addingColumn);
                        break;
                    }
                    case EControl.CHECKBOX:
                    {
                        DataGridViewCheckBoxColumn addingColumn = new DataGridViewCheckBoxColumn(false);
                        addingColumn.HeaderText = header.ColumnLabel;
                        this.Columns.Add(addingColumn);
                        break;
                    }
                    case EControl.DICTIONARY:
                    {
                        DataGridViewComboBoxColumn addingColumn = new DataGridViewComboBoxColumn();
                        addingColumn.HeaderText = header.ColumnLabel;
                        this.Columns.Add(addingColumn);
                        break;
                    }
                }
            }

            int rowIndex = 0;

            foreach (List<object> data in this._Collection.getRowData())
            {
                this.Rows.Add();
                for (int i = 0; i < data.Count; i++)
                {
                    switch (columnHeaders[i].ColumnType)
                    {
                        case EControl.TEXT:
                            this.Rows[rowIndex].Cells[i].Value = (string)data[i];
                            break;
                        case EControl.CHECKBOX:
                            this.Rows[rowIndex].Cells[i].Value = (bool)data[i];
                            break;
                        case EControl.OBJECT:
                            this.Rows[rowIndex].Cells[i].Value = data[i];
                            break;
                        case EControl.DICTIONARY:
                            List<string> dictionaryData = new List<string>();
                            string selectedValue = "";
                            foreach(KeyValuePair<string,bool> temp in ((List<KeyValuePair<string, bool>>)data[i]))
                            {
                                dictionaryData.Add(temp.Key);
                                if (temp.Value) selectedValue = temp.Key;
                            }
                            DataGridViewComboBoxCell cell =  ((DataGridViewComboBoxCell)this.Rows[rowIndex].Cells[i]);
                            cell.DataSource = dictionaryData;
                            cell.Value = selectedValue;
                            break;
                    }
                }
                rowIndex++;
            }
        }

        public int FindObjectIndex()
        {
            int i = 0;
            foreach (ExtensionGridColumnHeader column in this._Collection.GetColumnsType())
            {
                switch (column.ColumnType)
                {
                    case EControl.OBJECT:
                        return i;
                    default:
                        i++;
                        break;
                }
            }
            return -1;
        }
    }
}